//
//  QSBaseNavigationController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/17.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSBaseNavigationController.h"

@interface QSBaseNavigationController ()

@end

@implementation QSBaseNavigationController

+ (void)initialize
{
    UINavigationBar *bar = [UINavigationBar appearance];
    
    [bar setBackgroundImage:[UIImage imageWithFrame:CGRectMake(0, 0, kScreenWidth, kNavgationBarHeight) color:[UIColor QSColorBlue3F7DEF]] forBarMetrics:UIBarMetricsDefault];
    bar.shadowImage = [UIImage new];
    //设置渐变图层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, kScreenWidth, kNavgationBarHeight);
    gradientLayer.startPoint = CGPointMake(0.5, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#4E73F6"].CGColor,
                             (__bridge id)[UIColor colorWithHexString:@"#28A6FF"].CGColor];
    gradientLayer.locations = @[@(0.5f), @(1.0f)];
    UIView *navBarView = [[UIView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kNavgationBarHeight)];
    [navBarView.layer addSublayer:gradientLayer];
    //把View转换为Image
    UIImage *navImage = [UIImage convertViewToImage:navBarView];
    [bar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
    
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor QSColorWhiteFFFFFF] ,NSFontAttributeName : [UIFont QSBoldFontOfSize18]}];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor QSColorWhiteFFFFFF]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    FPSLabel *fps = [[FPSLabel alloc] initWithFrame:CGRectMake(10, 0, 50, 20)];
//    [self.navigationBar addSubview:fps];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
        //设置白色返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_navigation_back"] target:self action:@selector(back)];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    // 判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self popViewControllerAnimated:YES];
    }
}

@end
